<?
if ($data['modulename'] != '') {
if ($userdata['type'] != 'demo') {
	$reload_modules = false;
	switch ($data['action']) {
		case 'register':

			// Register dependencies
			$depres = true;
			if (stristr($data['module_depends'], ' ')) {
				$dependencies = explode(' ', $data['module_depends']);
				foreach($dependencies as $val) {
					$depres = $depres && register_module($val.'.mod.phps');
				}
			} elseif ($data['module_depends'] != '') {
				$depres = $depres && register_module($data['module_depends'].'.mod.phps');
			}

			if (!$depres) {
				$confirm[] = 'Could not register required dependencies. Please remove any modules that exclude them.';
			}

			// Unregister exclusions
			$excres = true;
			if (stristr($data['module_excludes'], ' ')) {
				$exclusions = explode(' ', $data['module_excludes']);
				$confirm[] = "Please unregister the following modules before continuing:";
				foreach($exclusions as $val) {
					//$excres = $excres && unregister_module($val.'.mod.phps');
					echo "$val";
				}
			} elseif ($data['module_excludes'] != '') {
					$confirm[] = "Please unregister the following modules before continuing: ".$data['module_excludes'];
					$excres = $excres && unregister_module($data['module_excludes'].'.mod.phps');
			}

			if (!$excres) {
				$confirm[] = 'Could not unregister required exclusions. Please remove any modules that depend on them.';
			}

			if ($depres && $excres) {
				if (register_module($data['modulename']))
					$confirm[] = $T['Modules']['Module Installed'];
				elseif (!check_dependencies($data['modulename'],'register'))
					$confirm[] = $T['Modules']['Err']['Exclusion Exists'];
			}
			break;
		case 'enable':
			// first deal with deps
			if (stristr($data['module_depends'], ' ')) {
				$dependencies = explode(' ', $data['module_depends']);
				foreach($dependencies as $val) {
					enable_module($val.'.mod.phps');
				}
			} elseif ($data['module_depends'] != '') {
				enable_module($data['module_depends'].'.mod.phps');
			}
			if (enable_module($data['modulename'])) {
				$confirm[] = $T['Modules']['Module Enabled'];
				$reload_modules = true;
			} elseif (!check_dependencies($data['modulename'], 'enable'))
				$confirm[] = $T['Modules']['Err']['Enable Exclusion Exists'];
			break;
		case 'disable':
			// nothing should stop it being disabled... but if there are dependencies
			// they will be found after trying to disable
			if (disable_module($data['modulename'])) {
				$confirm[] = $T['Modules']['Module Disabled'];
				$reload_modules = true;
			} elseif (!check_dependencies($data['modulename'], 'disable'))
				$confirm[] = $T['Modules']['Err']['Disable Dependency Exists'];
			break;
		case 'unregister':
			if (unregister_module($data['modulename']))
				$confirm[] = $T['Modules']['Module Uninstalled'];
			elseif (!check_dependencies($data['modulename'], 'unregister'))
				$confirm[] = $T['Modules']['Err']['Dependency Exists'];
			break;
	}
	if($reload_modules)
		commit('reload-modules');
} else {
	$confirm[] = $T['err']['demo user'];
}
}
if (is_array($confirm)) {
	foreach($confirm as $val) {
		echo '<div align="left"><b><br>&nbsp;&nbsp;'.$val.'</b></div><br>';
	}
}
?>
<style type="text/css">
<!--
/* PopUp Help Style */
.popup {
	border: solid 1px #666666;
	padding: 1px;
	/*width: 200;*/
	background-color: #94B4D1;
	layer-background-color: #94B4D1;
	position: absolute;
	visibility: hidden;
	text-align: left;
}
-->
</style>
<div align="center">
<table border="0" summary="" width="450" align="center">
<tr>
	<td align="right"><b>Module Name</b></td>
	<td align="center"><b>Installed</b></td>
	<td align="center"><b>Enabled</b></td>
</tr>
<?

// List all modules
$modules = dir_list($cfg['basedir'].'/modules', 'file');

// Sort list of modules into alphabetical order
sort($modules);

foreach($modules as $modname) {
	$modnamearry = explode('/', $modname);
	$modname = $modnamearry[sizeof($modnamearry) - 1];
	if (strpos($modname, '.mod.phps')) {
		unset($module);

		// Replace module file name with module definition file name
		$module_def = str_replace('.mod.phps', '.def.phps', $modname);

		// Set action so that nothing happens.
		$action = '';

		// Include module definition file
		include($cfg['basedir'].'/modules/'.$module_def);

		$SQL = 'SELECT name, state, dependencies FROM modules WHERE file = "'.$modname.'"';
		$res = mysql_query($SQL);
		if (mysql_num_rows($res) > 0) {
			$row = mysql_fetch_array($res);
		}

?>
<div id='<?=$modname;?>' class='popup'>
	<b><?=$module['name'];?></b><br>
	Description: <i><?=$module['description'];?></i><br>
	<?
	if (is_array($module['depends'])) {
		echo 'Dependencies: <i>'.implode(', ', $module['depends']).'</i><br>';
	}
	if (is_array($module['excludes'])) {
		echo 'Exclusions: <i>'.implode(', ', $module['excludes']).'</i><br>';
	}
	?>
</div>
<tr>
	<td align="right">
	<?
	if (mysql_num_rows($res) == 0) {
		echo "<span onmouseover=\"popup(event,'$modname')\" onmouseout=\"popup(event,'$modname')\">";
		echo $module['name'];
		echo "</span>";
	} else {
		echo "<span onmouseover=\"popup(event,'$modname')\" onmouseout=\"popup(event,'$modname')\">";
		echo $row['name'];
		echo "</span>";
	}
	?>
	</td>
	<td align="center">
	<?
	if (count($module['depends']) > 0)
		$module['depends'] = implode(' ', $module['depends']);
	if (count($module['excludes']) > 0)
		$module['excludes'] = implode(' ', $module['excludes']);

	if (mysql_num_rows($res) == 0) {
		echo '<a href="'.$current_url.'&amp;data[modulename]='.$modname.'&amp;data[action]=register&amp;data[module_depends]='.$module['depends'].'&amp;data[module_excludes]='.$module['excludes'].'"><img src="icon/down.gif" border="0" width="16" height="16"></a>';
	} elseif ($row['state'] == 1) {
		echo '<img src="icon/up.gif" border="0" width="16" height="16">';
	} else {
		echo '<a href="'.$current_url.'&amp;data[modulename]='.$modname.'&amp;data[action]=unregister"><img src="icon/up.gif" border="0" width="16" height="16"></a>';
	}
	?>
	</td>
	<td align="center">
	<?
	if (mysql_num_rows($res) == 0) {
		echo '<img src="icon/down.gif" border="0" width="16" height="16">';
	} elseif ($row['state'] == 1) {
		echo '<a href="'.$current_url.'&amp;data[modulename]='.$modname.'&amp;data[action]=disable"><img src="icon/up.gif" border="0" width="16" height="16"></a></td>';
	} else {
		echo '<a href="'.$current_url.'&amp;data[modulename]='.$modname.'&amp;data[action]=enable&amp;data[module_depends]='.$module['depends'].'&amp;data[module_excludes]='.$module['excludes'].'"><img src="icon/down.gif" border="0" width="16" height="16"></a></td>';
	}
	?>
	</td>
</tr>
<? }
}?>
</table>
<br>
</div>
